package com.umeox.watch.moto.agora;


import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Service;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.IBinder;
import android.telecom.TelecomManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.util.Pair;

import com.android.internal.telephony.ITelephony;
import com.umeox.watch.moto.dataservice.DataProvider;

import java.lang.reflect.Method;

public class Utils {


    /**
     * 获取未接视频电话、VOIP电话记录
     */
    public static Pair<Long, Boolean> getLastVideoCallsLog(String friendId) {
        boolean missed = false;
        long lastTime = 0;
        ContentValues callLogs = DataProvider.getLastVideoCallLogs(friendId);
        if (callLogs != null) {
            long duration = (long) callLogs.get(DataProvider.VideoCallProvider.CALL_DURATION);
            boolean newCall = (int) callLogs.get(DataProvider.VideoCallProvider.NEW_CALL) == 1;
            missed = duration == 0 && newCall;
            lastTime = (long) callLogs.get(DataProvider.VideoCallProvider.END_TIME);
        }
        return new Pair<>(lastTime, missed);
    }

    /**
     * 获取上次通话记录，并且通话时长大于30秒
     */
    public static long getLastValidVideoCallsLog(String friendId) {
        ContentValues callLogs = DataProvider.getLastVideoCallLogs(friendId);
        if (callLogs != null) {
            long duration = (long) callLogs.get(DataProvider.VideoCallProvider.CALL_DURATION);
            if (duration > 30 * 1000) {
                return (long) callLogs.get(DataProvider.VideoCallProvider.END_TIME);
            }
        }
        return 0;
    }

    /**
     * 是否正在普通通话中
     */
    public static boolean isPhoneIdle(Context context) {
        if (BuildConfig.DEBUG) {
            Log.w("Utils", "系统当前版本：" + Build.VERSION.SDK_INT);
        }

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            return !isInCall(context);
        }
        boolean isIdle = false;
        try {
            //反射获得系统服务的getService方法对象
            Method method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
            //执行这个方法得到一个IBinder对象
            IBinder binder = (IBinder) method.invoke(null, new Object[]{"phone"});
            //转换为具体的服务类(ITelephony)接口对象
            ITelephony iTelephony = ITelephony.Stub.asInterface(binder);
            if (iTelephony != null) {
                isIdle = iTelephony.isIdle();
            }
        } catch (Exception e) {
        }
        return isIdle;
    }

    @SuppressLint("MissingPermission")
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private static boolean isInCall(Context context) {
        TelecomManager tm = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
        return tm.isInCall();
    }


    /**
     * 挂断电话
     */
    public static void endCall(Context ctx) {
        TelephonyManager mTelMgr = (TelephonyManager) ctx.getSystemService(Service.TELEPHONY_SERVICE);
        Class<TelephonyManager> c = TelephonyManager.class;
        try {
            @SuppressLint("PrivateApi")
            Method getITelephonyMethod = c.getDeclaredMethod("getITelephony", (Class[]) null);
            getITelephonyMethod.setAccessible(true);
            ITelephony iTelephony;
            iTelephony = (ITelephony) getITelephonyMethod.invoke(mTelMgr, (Object[]) null);
            iTelephony.endCall();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static SharedPreferences getSpfs(Context context) {
        return context.getSharedPreferences("video_config", Context.MODE_PRIVATE);
    }

    public static int getVolume(Context context) {
        int defVolume = 200;
        SharedPreferences spf = getSpfs(context);
        if (spf != null) {
            return spf.getInt("current_volume", defVolume);
        }
        return defVolume;
    }

    public static void setVolume(Context context, int volume) {
        SharedPreferences spf = getSpfs(context);
        if (spf != null) {
            spf.edit().putInt("current_volume", volume).apply();
        }
    }
}
